//
//  JsBundleManager.m
//  JsoUtilitySdk
//
//  Created by Gikki Ares on 2021/8/6.
//

#import "JsBundleManager.h"

#define JsUtilityBundleId @"JsUtilitySdkBundle"

@implementation JsBundleManager

static NSBundle * _jsUtilityBundle;
static NSBundle * _iconBundle;

+ (void)load {
		//情况一 静态库引入, JsoUtilitySdk.bundle直接位于mainBunle下
	_jsUtilityBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:JsUtilityBundleId ofType:@"bundle"]];
		//情况二,pod引入,此时JsoUtilitySdk.bundle在framework里面,动态库引入.
	if(!_jsUtilityBundle) {
		NSString * bundlePath = [[NSBundle bundleForClass:[JsBundleManager class]] pathForResource:JsUtilityBundleId ofType:@"bundle"];
		_jsUtilityBundle = [NSBundle bundleWithPath:bundlePath];
	}
	_iconBundle = [self subBundleInBundle:_jsUtilityBundle withDirName:@"Icon.bundle" ];
}

+ (NSBundle *)jsUtilityBundle {
	if(!_jsUtilityBundle) {
			//情况一 静态库引入, JsoUtilitySdk.bundle直接位于mainBunle下
		_jsUtilityBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:JsUtilityBundleId ofType:@"bundle"]];
			//情况二,pod引入,此时JsoUtilitySdk.bundle在framework里面,动态库引入.
		if(!_jsUtilityBundle) {
			NSString * bundlePath = [[NSBundle bundleForClass:[JsBundleManager class]] pathForResource:JsUtilityBundleId ofType:@"bundle"];
			_jsUtilityBundle = [NSBundle bundleWithPath:bundlePath];
		}
		_iconBundle = [self subBundleInBundle:_jsUtilityBundle withDirName:@"Icon.bundle" ];
	}
	return _jsUtilityBundle;
}
+ (NSBundle *)jsMjRefreshBundle {
	static NSBundle * _jsMjRefreshBundle;
	if(!_jsMjRefreshBundle) {
		_jsMjRefreshBundle = [NSBundle bundleWithPath:[[self jsUtilityBundle] pathForResource:@"JsMjRefresh" ofType:@"bundle"]];
	}
	return _jsMjRefreshBundle;
}

+ (UIImage *)iconInBundleNamed:(NSString *)imageName {
	return [UIImage imageNamed:imageName inBundle:_iconBundle compatibleWithTraitCollection:nil];
}

//bundle中的一个目录可以被认为是一个子bundle
+ (UIImage *)iconInBundleNamed:(NSString *)imageName inDir:(NSString *)dirName {
	NSString * bundlePath = [_iconBundle pathForResource:dirName ofType:nil];
	NSBundle * bundle = [NSBundle bundleWithPath:bundlePath];
	return [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
}
//subbundlepath=bundlepath/dirName
+ (NSBundle *)subBundleInBundle:(NSBundle *)bundle withDirName:(NSString *)dirName {
	NSString * bundlePath = [bundle pathForResource:dirName ofType:nil];
	bundle = [NSBundle bundleWithPath:bundlePath];
	return bundle;
}

+ (NSString *)resoucePathInBundleNamed:(NSString *)resourceName inDir:(NSString *)dirName {
	NSBundle * bundle = nil;
	if(dirName) {
	NSString * bundlePath = [[self jsUtilityBundle] pathForResource:dirName ofType:nil];
	bundle = [NSBundle bundleWithPath:bundlePath];
	}
	return [bundle pathForResource:resourceName ofType:nil];
	
}

+ (UINib *)viewControllerNibWithName:(NSString *)name {
	return [UINib nibWithNibName:name bundle:[self jsUtilityBundle]];
}

@end
